<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* Short and sweet */
define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'].'/wp-blog-header.php');
//require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
//require_once($_SERVER['DOCUMENT_ROOT']."/wp-config.php");
//echo $_SERVER['DOCUMENT_ROOT'];

global $wpdb;

if(!is_user_logged_in()) {
    exit('Erreur');
}

global $display_name , $user_email;
get_currentuserinfo();

if(get_option('wpspo_emails_settings')) { extract(get_option('wpspo_emails_settings')); }
$paramEmail = get_option('wpspo_emails_settings');


if($_GET['data']) {
    
    $data = base64_decode($_GET['data']);
    $data = explode('##', $data);

    $leDefi = WPSPO_Defis::get($data[0]);
    //print_r($leDefi);
    $optionsDefi = WPSPO_Defis::get_optionsById($data[0], $current_user->ID);
    $valueOptions = json_decode($optionsDefi->defis_option_value);
    
    if($data[1]!=$valueOptions->cle_defis) {
        exit('ERREUR');
    }
    
} else {
    exit('Erreur');
}

    
if( !empty($_POST) && $_POST["posteinvite"]==true) {

    //Check si le titre n'est pas vide
    if(trim($_POST['email_invite']) == '') {
        $textError = 'Veuillez indiquer un email';
        $hasError = true;
    } else {
        $emailInvite = stripslashes(trim($_POST['email_invite']));
    }
        // Check security wordpress
    if ( !isset( $_POST['security'] ) || !wp_verify_nonce( $_POST['security'], 'action_post_invite' ) ) { 
        $securityError = __('Erreur de sécurité', 'sponsorpress');
        $textError = $securityError;
        $hasError = true;
    }

    // Si il n'y a pas d'erreur, on insère dans les tables et on envoi le mail
    if(!isset($hasError)) {
        
        $email_content = nl2br( stripslashes( wp_kses ( $paramEmail['email_txt_invite'], $_GLOBAL['allowedtags'] ) ) );
        $args = array(
            '#_CLE' => $valueOptions->cle_defis;
        );
        $email_content = wpspo_content_traitement($email_content, $args);
        

        $emailTo = sanitize_email( $_POST['email_invite'] );
        $subject = get_bloginfo('name').' - '.__( 'Participation in a challenge' , 'sponsorpress');
        $headers = array();
        $headers[] = 'FROM : ' . get_bloginfo('name') . ' <' . $paramWpspoSetting['emailplugin'] .'>' . "\r\n";
        $headers[] = "Reply-To: ".$paramWpspoSetting['emailplugin'];
        $headers[] = "Bcc: ".$paramWpspoSetting['emailplugin']."";

        add_filter('wp_mail_content_type',create_function('', 'return "text/html"; '));
        if( wp_mail( $emailTo, $subject, stripslashes($email_content), $headers ) ) {
             $emailSent = true;
        } else {
            //echo $emailTo.' -->'.$subject.' -->'.$email_content.' -->'.$headers.' --> '.$paramWpspoSetting['emailplugin']; 
            exit('Erreur envoi mail');
        }
        // Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
        remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
        function set_html_content_type() {
            return 'text/html';
        }

    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href='<?php echo get_stylesheet_directory_uri(); ?>/style.css' type='text/css' rel='stylesheet'/>
    <?php //wp_head(); ?>
    <style>
        .wpspo-erreur {
            color:#FFFFFF;
            border: 1px solid #990000;
            text-align: center;
            background-color:#990000;
            padding:3px;
            font-size: 1.075em;
            border-radius: 3px;
            margin-bottom: 15px;
        }
        .wpspo-confirm {
            color:#FFFFFF;
            border: 1px solid #008000;
            text-align: center;
            background-color:#008000;
            padding:3px;
            font-size: 1.075em;
            border-radius: 3px;
            margin-bottom: 15px;
        }
        .wpspoforms {
            text-align: center;
        }
    </style>
</head>

    <body>
        <div style="margin:15px;">
        <?php 
        if($emailSent==true) {
                echo '<div class="wpspo-confirm">L\'invitation a bien été envoyée</div>';
                $emailSent==false;
        }
          
        ?>
        <?php if($textError != '') { ?>
            <div class="wpspo-erreur"><?php echo $textError; ?></div> 
        <?php } ?>
        <p>
            Vous souhaitez envoyer une invitation à rejoindre votre défi :<br /><br />
            <div style="text-align:center;margin:15px;font-size: 18px;"><strong><i><?php echo $leDefi->post_title; ?></i></strong></div>
            <br /><br />La clé envoyée à votre invité est la suivante : <?php echo $valueOptions->cle_defis; ?><br /><br />
        </p>
        <form method="post" name="add_invite" action="#forminvite">
            <ol class="wpspoforms">

            <li><br /><br />
                <label for="title_defis">E-mail de votre invité :</label><br />           
                <input type="text" size="40" name="email_invite" id="email_invite" value="" style="font-size: 14px;" />

            </li>

            <li class="buttons">
                <INPUT type="hidden" name="posteinvite" value="true">
                <?php wp_nonce_field('action_post_invite','security'); ?>
                    <br /><br /><input type="submit" value="Envoyer" />
            </li>
           


            </ol>
        </form>
        </div>
</body>
</html>